package com.masterclient.app.modules.aftersale;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.eoner.baselibrary.bean.aftersale.IncomeCommisssionFilterBean;
import com.masterclient.app.R;
import com.masterclient.app.modules.aftersale.adapter.AfterSaleServePopAdapter;

import java.util.List;


public class AfterSaleServePopupWindow extends PopupWindow {
    private List<IncomeCommisssionFilterBean> tabBeans;

    public interface CallBack{
        void dismiss();
        void OnSelect(IncomeCommisssionFilterBean tabBean);
    };

    private CallBack callBack;

    AfterSaleServePopupWindow(Activity context, List<IncomeCommisssionFilterBean> tabBeans, CallBack callBack) {
        super(context);
        this.callBack = callBack;
        this.tabBeans = tabBeans;
        initView(context, tabBeans);
    }

    private void initView(final Activity context,List<IncomeCommisssionFilterBean> tabBeans) {
        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View mView = mInflater.inflate(R.layout.after_sale_select_popupwindow, null);
        RecyclerView recyclerView = mView.findViewById(R.id.recyclerView);
        TextView tvBg = mView.findViewById(R.id.tv_bg);
        tvBg.setOnClickListener(v -> dismiss());
        GridLayoutManager layoutManage = new GridLayoutManager(context, 3){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        recyclerView.setLayoutManager(layoutManage);
        AfterSaleServePopAdapter adapter = new AfterSaleServePopAdapter();
        recyclerView.setAdapter(adapter);
        adapter.setNewData(tabBeans);

        adapter.setOnItemClickListener((adapter1, view, position) -> callBack.OnSelect(tabBeans.get(position)));


        //设置SelectPicPopupWindow的View
        this.setContentView(mView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(WindowManager.LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        //设置PopupWindow可触摸
        this.setTouchable(true);
        //设置非PopupWindow区域是否可触摸


        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        this.setOnDismissListener(() -> {
            // TODO Auto-generated method stub
        });
    }


    @Override
    public void showAsDropDown(View anchor) {
        if (Build.VERSION.SDK_INT >= 24) {

            Rect rect = new Rect();
            anchor.getWindowVisibleDisplayFrame(rect);
            Activity activity = (Activity) anchor.getContext();
            Rect outRect1 = new Rect();
            activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect1);
            int h = outRect1.height() - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        callBack.dismiss();
    }
}





